package com.gxa.springboot229.service.impl;

import com.gxa.springboot229.excption.BusinessException;
import com.gxa.springboot229.mapper.ManagerMapper;
import com.gxa.springboot229.pojo.Manager;
import com.gxa.springboot229.service.ManagerService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("managerService")
public class ManagerServiceImpl implements ManagerService {
    @Resource
    ManagerMapper managerMapper;

    /**
     * 管理员查询接口
     * @param manager
     * @return
     */
    @Override
    public List<Manager> find (Manager manager) {
        List<Manager> managerList = managerMapper.select(manager);
        return managerList;
    }

    /**
     * 查询管理员基本信息
     * @param phone
     * @return
     */
    @Override
    public List <Manager> findMy (String phone) {
        Manager manager = new Manager();
        manager.setPhone(phone);
        List <Manager> select = managerMapper.select(manager);
        select.get(0).setRegDate(null);
        select.get(0).setState(null);
        return select;
    }

    @Override
    public List <Manager> findMy (int id) {
        Manager manager = new Manager();
        manager.setId(id);
        List <Manager> select = managerMapper.select(manager);
        select.get(0).setRegDate(null);
        select.get(0).setState(null);
        return select;
    }

    /**
     * 管理员登录接口
     * @param phone
     * @return
     */
    @Override
    public int Login (String phone) {
        Manager manager = new Manager();
        manager.setPhone(phone);
        return managerMapper.selectCount(manager);
    }

    /**
     * 添加管理员
     * @param name
     * @param phone
     * @return
     */
    @Override
    public int add (String name, String phone) {
        Manager manager = new Manager();
        manager.setPhone(phone);
        //查询操作 比对操作
        int ph = managerMapper.selectCount(manager);
        if(ph > 0){
            throw  new BusinessException("此电话以存在");
        }

        //添加操作
        manager.setName(name);
        manager.setRegDate(new Date());
        manager.setState(0);
        return  managerMapper.insertSelective(manager);
    }

    @Override
    public int del (String phone) {
        Manager manager = new Manager();
        manager.setPhone(phone);
        return managerMapper.delete(manager);
    }

    @Override
    public int update (Manager manager) {
        manager.setRegDate(null);
        return managerMapper.updateByPrimaryKeySelective(manager);
    }
}
